package com.jinbo.footmessage.setting;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.UserInformationBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.login.LoginActivity;
import com.jinbo.footmessage.utils.ActivitiesHelper;
import com.jinbo.footmessage.utils.DialogUtils;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.utils.VerifyUtils;
import com.jinbo.footmessage.view.CrossEditText;
import com.jinbo.footmessage.view.TitleBarView;


public class ModifyPwdActivity extends BaseActivity implements View.OnClickListener{

    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, ModifyPwdActivity.class);
        activity.startActivity(intent);
    }
    private Button submit;
    private CrossEditText edt_oldpwd,edt_newpwd,edt_renewpwd;
    private CheckBox mShowPasswordCb;
    private TitleBarView tbv_titleBar;
   // private LocalPreferencesHelper mLocalPreferencesHelper;
    private String user_id;
    private String mPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        initTitleBarView();
        initView();
        initData();
        initListener();

    }
    private void initData(){
        user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        mPwd=MyApplication.local.getString(user_id+LocalPreferencesHelper.USER_PASSWORD);
 /*       mLocalPreferencesHelper = new LocalPreferencesHelper(this,LocalPreferencesHelper.DB_NAME);
        mAccess_token = mLocalPreferencesHelper.getString(LocalPreferencesHelper.ACCESS_TOKEN);
        mPwd = mLocalPreferencesHelper.getString(LocalPreferencesHelper.USER_PASSWORD);*/
    }
    public void initTitleBarView() {
        tbv_titleBar = (TitleBarView) findViewById(R.id.tbv_setting);
        tbv_titleBar.setCenterTitle("修改密码");
        tbv_titleBar.setLeftBackButtonIsVisibler(true);
        tbv_titleBar.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
    }

    // 初始化View
    private void initView() {
        submit = (Button) findViewById(R.id.submit);
        submit.setEnabled(false);
        edt_oldpwd = (CrossEditText) findViewById(R.id.edt_oldpwd);
        edt_newpwd = (CrossEditText) findViewById(R.id.edt_newpwd);
        edt_renewpwd = (CrossEditText) findViewById(R.id.edt_renewpwd);
        mShowPasswordCb = (CheckBox) findViewById(R.id.show_password_chb);
    }
    // 监听
    private void initListener() {
        submit.setOnClickListener(this);
        mShowPasswordCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    edt_oldpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    edt_newpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    edt_renewpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    int length = edt_oldpwd.getText().length();
                    Selection.setSelection(edt_oldpwd.getText(), length);
                    int length2 = edt_newpwd.getText().length();
                    Selection.setSelection(edt_newpwd.getText(), length2);
                    int length3 = edt_renewpwd.getText().length();
                    Selection.setSelection(edt_renewpwd.getText(), length3);
                } else {
                    edt_oldpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    edt_newpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    edt_renewpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    int length = edt_oldpwd.getText().length();
                    Selection.setSelection(edt_oldpwd.getText(), length);
                    int length2 = edt_newpwd.getText().length();
                    Selection.setSelection(edt_newpwd.getText(), length2);
                    int length3 = edt_renewpwd.getText().length();
                    Selection.setSelection(edt_renewpwd.getText(), length3);
                }
            }
        });
        edt_oldpwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edt_oldpwd.getText().toString().trim().length() > 0
                        && edt_newpwd.getText().toString().trim().length() > 0
                        && edt_renewpwd.getText().toString().trim().length() > 0) {
                    submit.setEnabled(true);
                } else {
                    submit.setEnabled(false);
                }
            }
        });
        edt_newpwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edt_oldpwd.getText().toString().trim().length() > 0
                        && edt_newpwd.getText().toString().trim().length() > 0
                        && edt_renewpwd.getText().toString().trim().length() > 0) {
                    submit.setEnabled(true);
                } else {
                    submit.setEnabled(false);
                }
            }
        });
        edt_renewpwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edt_oldpwd.getText().toString().trim().length() > 0
                        && edt_newpwd.getText().toString().trim().length() > 0
                        && edt_renewpwd.getText().toString().trim().length() > 0) {
                    submit.setEnabled(true);
                } else {
                    submit.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                check();
                break;
            case R.id.left_back_layout:
                finish();
                break;
            default:
                break;
        }
    }

    private void check() {
        String oldpwd=edt_oldpwd.getText().toString().trim();
        final String newpwd=edt_newpwd.getText().toString().trim();
        String renewpwd=edt_renewpwd.getText().toString().trim();

      /*  if(TextUtils.isEmpty(oldpwd) ) {
            new DialogUtils(ModifyPwdActivity.this, "center", true)
                    .setMessage("请输入原密码")
                    .setButtons(null, "确定", null).create().show();
            return;
        }
        if(TextUtils.isEmpty(newpwd)){
            new DialogUtils(ModifyPwdActivity.this, "center",true)
                    .setMessage("请输入新密码")
                    .setButtons(null, "确定",null).create().show();
            return;
        }
        if(TextUtils.isEmpty(renewpwd)){
            new DialogUtils(ModifyPwdActivity.this, "center",true)
                    .setMessage("请输入确认密码")
                    .setButtons(null, "确定",null).create().show();
            return;
        }*/
        if(!newpwd.equals(renewpwd)){
            new DialogUtils(ModifyPwdActivity.this, "center",true,0)
                    .setMessage("确认密码与新密码不一致")
                    .setButtons(null, "确定", null)
                    .create().show();
            return;
        }
  /*      if(!oldpwd.equals(mPwd)){
            new DialogUtils(ModifyPwdActivity.this, "center",true,0)
                    .setMessage("原密码输入错误")
                    .setButtons(null, "确定", null)
                    .create().show();
            return;
        }*/
        if(!VerifyUtils.isPassword(newpwd)|| newpwd.length() > 16 || newpwd.length() < 6){
            new DialogUtils(ModifyPwdActivity.this, "center",true,0)
                    .setMessage("密码格式为6-16位数字或字母组合")
                    .setButtons(null, "确定",null).create().show();
            return;
        }
        BusinessController.getInstance().modifyUserPassword(user_id,oldpwd, newpwd,new Listener<UserInformationBean>() {

            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
            }

            @Override
            public void onComplete(UserInformationBean result, Object... params) {
                MyApplication.local.saveOrUpdate(user_id+ LocalPreferencesHelper.USER_PASSWORD, newpwd);
                dismissProgressDialog();
                new DialogUtils(ModifyPwdActivity.this, "center",false,0)

                        .setMessage("密码修改成功，请您重新登录")
                        .setButtons("确定", "取消",
                                new DialogUtils.OnDialogButtonClickListener() {
                                    @Override
                                    public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                        if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {

                                        }
                                        if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_LEFT) {
                                          //  ActivitiesHelper.getInstance().closeAll();
                                            LoginActivity.launch(ModifyPwdActivity.this);
                                            finish();
                                            ActivitiesHelper.getInstance().closeAll();//, false);
                                        }
                                    }
                                }).create().show();
            }

            @Override
            public void onFail(String msg, Object... params) {

                if (TextUtils.isEmpty(msg)) {
                } else {
                    dismissProgressDialog();
                    new DialogUtils(ModifyPwdActivity.this, "center",true,0).setMessage(msg).setButtons(null, "确定", null).create().show();
                }
            }
        });
    }
}
